package com.cj.webapp.user.controller;

import com.cj.webapp.user.dto.UserDto;
import com.cj.webapp.user.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by dell on 2018/1/22.
 */
@WebServlet(name = "UserRegisterServlet")
public class UserRegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String gender=request.getParameter("gender");
        String age=request.getParameter("age");
        String phone=request.getParameter("phone");

        UserDto userDto=new UserDto();
        userDto.setName(name);
        userDto.setPassword(password);
        userDto.setGender(gender);
        userDto.setAge(age);
        userDto.setPhone(phone);

        UserServiceImpl service=new UserServiceImpl();
        int res = service.addUser(userDto);
        if(res==1){
            response.sendRedirect("/Login.html");
        }else {
            response.sendRedirect("/page/index/registerError.html");
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           doPost(request,response);
    }
}
